package model

import "gorm.io/gorm"

type Model struct {
	Id        uint `gorm:"primarykey"`
	CreatedAt Time `gorm:"type:time;<-:create"`
	UpdatedAt Time `gorm:"type:time"`
	DataFlag  int  `gorm:"default:1" json:"-"`
}

func (m *Model) GetId() uint {
	return m.Id
}

type State struct {
	Status int `gorm:"-"`
}

type Enable struct {
	Enabled bool
}

func (e *Enable) Update(db *gorm.DB) error {
	return db.Update("enabled", e.Enabled).Error
}

func (m *State) Change(status int) {
	m.Status = status
}

type SysConfig struct {
	Model
	Name string `gorm:"size:64;index" json:"name"`
	Desc string `gorm:"size:128" json:"desc"`
	// 数值类型专用
	NumVal int64 `json:"num_val"`
	// 浮点类型专用
	FloatVal float64 `json:"float_val"`

	Value string `gorm:"size:512" json:"value"`
}

func (s *SysConfig) FindByDB(db *gorm.DB) error {
	return db.Find(s, "name = ?", s.Name).Error
}

func (s *SysConfig) TableName() string {
	return "sys_configs"
}

type CornConfig struct {
	Model
	Enable
	Type  uint   `json:"type"`
	Log   bool   `json:"log"`
	Name  string `gorm:"size:64;index" json:"name"`
	Desc  string `gorm:"size:128" json:"desc"`
	Param string `gorm:"size:512" json:"param"`
	Corn  string `gorm:"size:64" json:"corn"`
}

func (c *CornConfig) TableName() string {
	return "corn_configs"
}

type DictType struct {
	Model
	Name string `gorm:"size:64;index" json:"name"`
	Desc string `gorm:"size:128" json:"desc"`
}

func (d *DictType) TableName() string {
	return "dict_types"
}

type Dict struct {
	Model
	Pid       int
	Type      int         `gorm:"index" json:"-"`
	Name      string      `gorm:"size:6" json:"name"`
	OrderNo   int32       `json:"order_no"`
	StringVal string      `gorm:"size:256" json:"string_val"`
	NumVal    int64       `json:"num_val"`
	Children  interface{} `gorm:"-"`
}

func (d *Dict) TableName() string {
	return "dicts"
}
